package model

import "gpt-api/framework/db"

type Relations struct {
	db.BaseModel
	Name    string `json:"name" gorm:"column:name;type:varchar(50);default:'';comment:关系名称"`
	Parent  int    `json:"parent" gorm:"column:parent;type:int;default:0;comment:父级关系"`
	OnlyOne int    `json:"only_one" gorm:"column:only_one;type:int;default:0;comment:是否唯一0:否1:是"`
}

// 定义关系ID常量
type RelationsID uint64

const (
	RelationsFather   RelationsID = 1  // 父亲
	RelationsMother   RelationsID = 2  // 母亲
	RelationsPartner  RelationsID = 3  // 伴侣
	RelationsDaughter RelationsID = 4  // 女儿
	RelationsSon      RelationsID = 5  // 儿子
	RelationsSisters  RelationsID = 6  // 姐妹
	RelationsBrother  RelationsID = 7  // 兄弟
	RelationsWife     RelationsID = 8  // 妻子
	RelationsExWife   RelationsID = 9  // 前妻
	RelationsObject   RelationsID = 10 // 对象
)

func (Relations) TableName() string {
	return "relations"
}

// 根据ID查询关系
func (r Relations) GetRelationsById(database *db.DB, id int) (Relations, error) {
	var relation Relations
	err := database.Model(&Relations{}).Where("id = ?", id).First(&relation).Error
	return relation, err
}

// 查询指定父级ID下的所有子关系
func (r Relations) GetChildren(database *db.DB, parent int) ([]Relations, error) {
	var relations []Relations
	err := database.Model(&Relations{}).Where("parent = ?", parent).Find(&relations).Error
	return relations, err
}

// 查询所有关系，并排除一部分ID
func (r Relations) GetRelationsNotId(database *db.DB, not_id []uint64) ([]Relations, error) {
	var relation []Relations
	if len(not_id) == 0 {
		not_id = []uint64{0}
	}
	err := database.Model(&Relations{}).Where("parent = ?", 0).Where("id NOT IN ?", not_id).Find(&relation).Error
	return relation, err
}

// 查询指定名称的关系
func (r Relations) GetRelationsForOnly(database *db.DB) ([]Relations, error) {
	var relations []Relations
	err := database.Model(&Relations{}).Where("only_one = ?", 1).Find(&relations).Error
	return relations, err
}
